{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "external-mayor",
   "metadata": {},
   "source": [
    "## print \n",
    "Python中的输出函数，用来打印给定的值   \n",
    "代码中，所有的符号都是英文的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "antique-exploration",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "desperate-engineer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "inner-jungle",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2\n"
     ]
    }
   ],
   "source": [
    "print(1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "devoted-ivory",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1.2\n"
     ]
    }
   ],
   "source": [
    "print(1,1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "knowing-incidence",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('hello','world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "congressional-lightweight",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'hello' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-1cd80308eb4c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhello\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'hello' is not defined"
     ]
    }
   ],
   "source": [
    "print(hello)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "refined-examination",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('hello world')\n",
    "print('hello world')\n",
    "print('hello world')\n",
    "print('hello world')\n",
    "print('hello world')\n",
    "print('hello world')\n",
    "print('hello world')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "inside-cream",
   "metadata": {},
   "source": [
    "## 变量\n",
    "为了方便对部分值进行管理，我们会设置变量，并将其赋值给特定的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "informational-origin",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world'\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "regulated-affiliation",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello kitty\n",
      "hello kitty\n",
      "hello kitty\n",
      "hello kitty\n",
      "hello kitty\n",
      "hello kitty\n"
     ]
    }
   ],
   "source": [
    "s = 'hello kitty'\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "private-sweet",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s\n",
      "s\n"
     ]
    }
   ],
   "source": [
    "s = 'hello kitty'\n",
    "print('s')\n",
    "print('s')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "japanese-status",
   "metadata": {},
   "source": [
    "Python中，文本类的东西，如果没有被引号包起来，   \n",
    "Python会将其认为变量，然后去找变量对应的值  \n",
    "如果找不到，就会报错，`NameError`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "altered-longitude",
   "metadata": {},
   "source": [
    "`s = 'hello kitty'`   \n",
    "一个等号，我们叫赋值语句   \n",
    "等号的左边是变量   \n",
    "等号的右边是变量的值   \n",
    "右边先执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "opened-narrative",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "s = 1 + 1\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "atomic-approval",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-709cf1746c3a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "s = a\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ignored-stopping",
   "metadata": {},
   "source": [
    "### 变量的命名规范   \n",
    "- 硬性要求：  \n",
    "1. 变量名中可以包含数字，但不能以数字作为开头  \n",
    "2. 变量名只能包含字母、数字、下划线   \n",
    "3. 关键字不能作为变量名   \n",
    "4. 变量名区分大小写   \n",
    "- 规范：     \n",
    "1. 定义变量名时，留意该变量有没有被使用过   \n",
    "2. 变量名尽量见其名，知其意   \n",
    "3. 变量名尽量用英文名称，小写   \n",
    "4. 多个单词组成的变量名，中间用_隔开\n",
    "5. 中文可以作为变量名，但你们最好当做不知道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "stable-textbook",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "s = 'a'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "experimental-professional",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "s1 = 'a'\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "engaged-opening",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-18-29ebed9b68ab>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-18-29ebed9b68ab>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    1s = 'a'\u001b[0m\n\u001b[1;37m     ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "1s = 'a'\n",
    "print(1s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beneficial-ensemble",
   "metadata": {},
   "source": [
    "规则1：变量名中可以包含数字，但不能以数字作为开头"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "seven-accent",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-19-8009d1a2f3fd>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-19-8009d1a2f3fd>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    s* = 'a'\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "s* = 'a'\n",
    "print(s*)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "incoming-proxy",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "s_ = 'a'\n",
    "print(s_)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "solid-offer",
   "metadata": {},
   "source": [
    "规则2：变量名只能包含字母、数字、下划线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "built-government",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "_s = 'a'\n",
    "print(_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "israeli-norwegian",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-22-c4c74c82b6a5>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-22-c4c74c82b6a5>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    if = 'a'\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "if = 'a'\n",
    "print(if)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adverse-clothing",
   "metadata": {},
   "source": [
    "规则3：关键字不能作为变量名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "proved-footage",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "import keyword\n",
    "print(keyword.kwlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "outer-thomson",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "A = 2\n",
    "print(a)\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "naughty-question",
   "metadata": {},
   "source": [
    "规则4： 变量名区分大小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "wrong-canvas",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "a = 2\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "physical-chapel",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "a = 'a'\n",
    "a = 2\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "arabic-worry",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "中 = 'a'\n",
    "print(中)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "vital-ultimate",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "python = 'a'\n",
    "print(python)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "based-carroll",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-29-d04368c86249>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-29-d04368c86249>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    python = \"\"hi\"\"\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "python = \"\"hi\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "correct-george",
   "metadata": {},
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "victorian-symphony",
   "metadata": {},
   "source": [
    "### 字符串\n",
    "用引号包起来，叫字符串  \n",
    "1. 单引号   \n",
    "2. 双引号   \n",
    "3. 三引号:可以保留输入格式，如换行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "departmental-billy",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "a = 'a'\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "yellow-mother",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "a = \"a\"\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "controlled-tyler",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "a = '''a'''\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "allied-pavilion",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "a = \"\"\"a\"\"\"\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "awful-population",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-34-eea08cdc28cf>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-34-eea08cdc28cf>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    a = 'he's'\u001b[0m\n\u001b[1;37m            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "a = 'he's'\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "raising-dallas",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "he's\n"
     ]
    }
   ],
   "source": [
    "a = \"he's\"\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "straight-cleveland",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "he\"s he's,her\"s\n"
     ]
    }
   ],
   "source": [
    "a = 'he\"s'\n",
    "b = '''he's,her\"s'''\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "focal-transparency",
   "metadata": {},
   "source": [
    "通过type()函数，可以查看变量的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "departmental-brave",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'a'\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "natural-engineer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = 'a'\n",
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "hundred-dialogue",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "stone-boating",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "accredited-university",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOL while scanning string literal (<ipython-input-42-cc322847a0f8>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-42-cc322847a0f8>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    s = '窗前明月光，\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
     ]
    }
   ],
   "source": [
    "s = '窗前明月光，\n",
    "疑似地上霜。\n",
    "举头望明月，\n",
    "低头思故乡。'\n",
    "\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "dense-remark",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "床前明月光，\n",
      "疑似地上霜。\n",
      "举头望明月，\n",
      "低头思故乡\n"
     ]
    }
   ],
   "source": [
    "s = '''床前明月光，\n",
    "疑似地上霜。\n",
    "举头望明月，\n",
    "低头思故乡'''\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "equipped-prerequisite",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\test0315\n"
     ]
    }
   ],
   "source": [
    "path = 'D:\\test0315'\n",
    "print(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "parental-trail",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d:\n",
      "ext\\word\n"
     ]
    }
   ],
   "source": [
    "path = 'd:\\next\\word'\n",
    "print(path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fleet-company",
   "metadata": {},
   "source": [
    "`\\`，称之为转义字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "hourly-dispatch",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d:\\next\\word\n"
     ]
    }
   ],
   "source": [
    "path = 'd:\\\\next\\word'\n",
    "print(path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "broad-person",
   "metadata": {},
   "source": [
    "第一个转义字符，将第二转义字符，转义成斜杠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "common-river",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "he's\n"
     ]
    }
   ],
   "source": [
    "s = 'he\\'s'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "portuguese-bulgarian",
   "metadata": {},
   "source": [
    "转义字符`\\`，将字符串标识`'`转义成单引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "analyzed-inside",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d:\\next\\word\n"
     ]
    }
   ],
   "source": [
    "path = r'd:\\next\\word'\n",
    "print(path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chronic-testimony",
   "metadata": {},
   "source": [
    "`r`的作用，取消将转义字符`\\`的作用，让其作为一个斜杠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "sacred-blues",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "he\\'s\n"
     ]
    }
   ],
   "source": [
    "s = r'he\\'s'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "empirical-cartridge",
   "metadata": {},
   "source": [
    "### 整型\n",
    "整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "elementary-phase",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "num = 1\n",
    "print(num)\n",
    "print(type(num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "manual-privilege",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "num = '1'\n",
    "print(num)\n",
    "print(type(num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "charged-canyon",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "a = 'abc'\n",
    "b = 'a'\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "accepting-cherry",
   "metadata": {},
   "source": [
    "### 浮点型\n",
    "小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "adolescent-thomson",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "num = 1.0\n",
    "print(num)\n",
    "print(type(num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "computational-melissa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "num = .2\n",
    "print(num)\n",
    "print(type(num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "available-motion",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "num = 2.\n",
    "print(num)\n",
    "print(type(num))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "economic-romance",
   "metadata": {},
   "source": [
    "## 类型的函数及转换\n",
    "### 字符串\n",
    "str: 将给定的值转为字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "expressed-module",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "a = str\n",
    "b = str()\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "upper-drain",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "mobile-airline",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "certified-dietary",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = str(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "returning-slave",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = 1.2\n",
    "b = str(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "monthly-bottle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.2'"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "defensive-alignment",
   "metadata": {},
   "source": [
    "### 整型\n",
    "int: 将给定的对象转为整型   \n",
    "1. 当对象是浮点型时，取整   \n",
    "2. 当对象是字符串时，只支持整型的字符串转为整型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "smoking-welcome",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a = '1'\n",
    "b = int(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "orange-world",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a = 1.2\n",
    "b = int(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "spoken-canal",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a = 1.8\n",
    "b = int(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "homeless-vacation",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '1.2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-70-6dccb3a78b14>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'1.2'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '1.2'"
     ]
    }
   ],
   "source": [
    "b = int(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "light-brother",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'a1'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-71-ae1591b296df>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'a1'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a1'"
     ]
    }
   ],
   "source": [
    "a = 'a1'\n",
    "b = int(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "orange-quebec",
   "metadata": {},
   "source": [
    "### 浮点型\n",
    "float: 将给定的对象转为浮点型   \n",
    "1. 给定的对象是数值型字符串，转为浮点型\n",
    "2. 非数值型，转不了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "regulated-mouse",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = float(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "civic-burning",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a = 1.2\n",
    "b = float(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "suffering-express",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a = '1.2'\n",
    "b = float(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "adjusted-double",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a = '1'\n",
    "b = float(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "renewable-perfume",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "could not convert string to float: 'a1'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-76-2c2a3483d3cd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'a1'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'a1'"
     ]
    }
   ],
   "source": [
    "a = 'a1'\n",
    "b = float(a)\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "absent-single",
   "metadata": {},
   "source": [
    "### 练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "thrown-enforcement",
   "metadata": {},
   "source": [
    "1. 指出以下变量的值及类型   \n",
    "\n",
    "```python\n",
    "a1 = int(0.)\n",
    "a2 = int('0.')\n",
    "a3 = int(9.999999)\n",
    "a = '10'\n",
    "a4 = int(a)\n",
    "a5 = int('a')\n",
    "a6 = int(8)\n",
    "\n",
    "a7 = float(0)\n",
    "a8 = float(0.)\n",
    "a9 = float('0')\n",
    "a10 = float('10')\n",
    "a11 = float('12.4')\n",
    "a12 = float('12.4a')\n",
    "a13 = float(a)\n",
    "a14 = float('a')\n",
    "\n",
    "a15 = str(0)\n",
    "a16 = str(0.)\n",
    "a17 = str('0.')\n",
    "a18 = str(a)\n",
    "a19 = str('a')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "distinct-estonia",
   "metadata": {},
   "source": [
    "2. 如何借助int函数实现四舍五入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "endless-testament",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(int(2.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "bacterial-evolution",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "print(int(2.5+0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "sitting-newport",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(int(2.4+0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "planned-cisco",
   "metadata": {},
   "source": [
    "## input\n",
    "输入函数   \n",
    "接收用户的输入，并返回   \n",
    "input中可传入字符串，作为输入时的提示语   \n",
    "不管输入的是什么，都会转为字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "sealed-bradley",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'zhangsan'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "massive-category",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n"
     ]
    }
   ],
   "source": [
    "name = input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "affecting-abuse",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "acting-velvet",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n"
     ]
    }
   ],
   "source": [
    "name = input('请输入姓名：')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "hydraulic-manner",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "honest-domain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数：2\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-87-a474766a0551>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入一个整数：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "num = input('请输入一个整数：')\n",
    "result = num + 1\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "straight-organization",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个小数：2.3\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-88-16baddc379be>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入一个小数：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "num = input('请输入一个小数：')\n",
    "result = num + 1\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "forbidden-party",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数：2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "num = input('请输入一个整数：')\n",
    "result = int(num) + 1\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "innocent-footage",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个浮点数：2.3\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '2.3'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-90-924320b007c3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入一个浮点数：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '2.3'"
     ]
    }
   ],
   "source": [
    "num = input('请输入一个浮点数：')\n",
    "result = int(num) + 1\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "final-measurement",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个浮点数：2.3\n",
      "3.3\n"
     ]
    }
   ],
   "source": [
    "num = input('请输入一个浮点数：')\n",
    "result = float(num) + 1\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "defined-study",
   "metadata": {},
   "source": [
    "输入圆的半径，输出圆的周长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "mighty-cycle",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入半径：0.5\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'float'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-92-ee47512d74eb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mradius\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入半径：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mpai\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m3.14\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mzc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mpai\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mradius\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mzc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'"
     ]
    }
   ],
   "source": [
    "radius = input('请输入半径：')\n",
    "pai = 3.14\n",
    "zc = 2 * pai * radius\n",
    "print(zc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "about-shaft",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入半径：0.5\n",
      "3.14\n"
     ]
    }
   ],
   "source": [
    "radius = input('请输入半径：')\n",
    "pai = 3.14\n",
    "zc = 2 * pai * float(radius)\n",
    "print(zc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "sustainable-contribution",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入半径：0.5\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'float'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-94-0a51314351ec>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入半径：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mzc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mpai\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mr\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mzc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'"
     ]
    }
   ],
   "source": [
    "r = input('请输入半径：')\n",
    "float(r)\n",
    "zc = 2 * pai * r\n",
    "print(zc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "southeast-sharing",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "r = '0.5'\n",
    "print(type(float(r)))\n",
    "print(type(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "colored-weekend",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14\n"
     ]
    }
   ],
   "source": [
    "r = '0.5'\n",
    "r = float(r)\n",
    "zc = 2 * pai * r\n",
    "print(zc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dominican-destination",
   "metadata": {},
   "source": [
    "## 字符串的格式化\n",
    "利用占位符格式字符串   \n",
    "1. `%s`：以字符串的方式进行填充，可接收任意类型\n",
    "2. `%f`：以浮点型的格式进行填充，只能接收数值型，可以指定小数位，如两位小数`%.2f`   \n",
    "3. `%d`：以整型的格式进行填充，只能接收数值型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "catholic-european",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n",
      "zhangsan 欢迎光临\n"
     ]
    }
   ],
   "source": [
    "s = '欢迎光临'\n",
    "name = input('请输入姓名：')\n",
    "print(name,s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "younger-intersection",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n",
      "zhangsan欢迎光临\n"
     ]
    }
   ],
   "source": [
    "s = '欢迎光临'\n",
    "name = input('请输入姓名：')\n",
    "print(name+s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "eastern-japanese",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n",
      "%s欢迎光临 zhangsan\n",
      "zhangsan欢迎光临\n"
     ]
    }
   ],
   "source": [
    "s = '%s欢迎光临'\n",
    "name = input('请输入姓名：')\n",
    "print(s,name)\n",
    "print(s % name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "central-projection",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "not enough arguments for format string",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-101-6ee85fa55198>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'欢迎%s莅临%s'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入姓名：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: not enough arguments for format string"
     ]
    }
   ],
   "source": [
    "s = '欢迎%s莅临%s'\n",
    "name = input('请输入姓名：')\n",
    "print(s % name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "acceptable-guard",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "not enough arguments for format string",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-102-72d0f76557a1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入姓名：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0maddress\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'泽林'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0maddress\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: not enough arguments for format string"
     ]
    }
   ],
   "source": [
    "s = '欢迎%s莅临%s'\n",
    "name = input('请输入姓名：')\n",
    "address = '泽林'\n",
    "print(s % name,address)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "invisible-mustang",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n",
      "欢迎zhangsan莅临泽林\n"
     ]
    }
   ],
   "source": [
    "s = '欢迎%s莅临%s'\n",
    "name = input('请输入姓名：')\n",
    "address = '泽林'\n",
    "print(s % (name,address))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "round-columbia",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入姓名：zhangsan\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "not all arguments converted during string formatting",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-104-13ee58d7eeb3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入姓名：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0maddress\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'泽林'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0maddress\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: not all arguments converted during string formatting"
     ]
    }
   ],
   "source": [
    "s = '欢迎%s莅临%s'\n",
    "name = input('请输入姓名：')\n",
    "address = '泽林'\n",
    "print(s % (name,address,name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "pleasant-implementation",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体重：120\n"
     ]
    }
   ],
   "source": [
    "s = '体重：%s'\n",
    "print(s % 120)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "other-teaching",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体重：12.0\n"
     ]
    }
   ],
   "source": [
    "s = '体重：%s'\n",
    "print(s % 12.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "attractive-brake",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体重：120.94847932749744\n"
     ]
    }
   ],
   "source": [
    "s = '体重：%s'\n",
    "print(s % 120.948479327497431)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "excellent-apache",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体重：120.948479\n"
     ]
    }
   ],
   "source": [
    "s = '体重：%f'\n",
    "print(s % 120.948479327497431)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "brief-trainer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体重：120.000000\n"
     ]
    }
   ],
   "source": [
    "s = '体重：%f'\n",
    "print(s % 120)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "outdoor-intake",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体重：120.95\n"
     ]
    }
   ],
   "source": [
    "s = '体重：%.2f'\n",
    "print(s % 120.948479327497431)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "transparent-strip",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：20\n"
     ]
    }
   ],
   "source": [
    "s = '年龄：%s'\n",
    "print(s % 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "refined-experience",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：20\n"
     ]
    }
   ],
   "source": [
    "s = '年龄：%d'\n",
    "print(s % 20.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "spectacular-cause",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "%d format: a number is required, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-114-516b4aeb480f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'年龄：%d'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;34m'zhangsan'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: %d format: a number is required, not str"
     ]
    }
   ],
   "source": [
    "s = '年龄：%d'\n",
    "print(s % 'zhangsan')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "controversial-floating",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "must be real number, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-115-f4847a77ce4f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'年龄：%f'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;34m'zhangsan'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: must be real number, not str"
     ]
    }
   ],
   "source": [
    "s = '年龄：%f'\n",
    "print(s % 'zhangsan')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "filled-session",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你的体重为：%d 65\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "%d format: a number is required, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-d50478daf4eb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'你的体重为：%d'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m65\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'你的体重为：%d'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;34m'65'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'你的体重为：%d'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'你的体重为：%d'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'你的体重为：%d，你的身高为：%d'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m65\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: %d format: a number is required, not str"
     ]
    }
   ],
   "source": [
    "print('你的体重为：%d',65)\n",
    "print('你的体重为：%d' % '65')\n",
    "print('你的体重为：%d' % True)\n",
    "print('你的体重为：%d' % False)\n",
    "print('你的体重为：%d，你的身高为：%d' % 65)\n",
    "print('你的体重为：%d，你的身高为：%d' % 65,75)\n",
    "print('你的体重为：%f' % 65)\n",
    "print('你的体重为：%f' % '65')\n",
    "print('你的体重为：%.2f' % 65)\n",
    "print('你的体重为：%.3f' % True)\n",
    "print('你的体重为：%f' % False)\n",
    "print('你的体重为：%.2f，你的身高为：%.2f' % (65,75))\n",
    "print('你的体重为：%sKG' % 65)\n",
    "print('你的体重为：%5sKG' % 65)\n",
    "print('你的体重为：%-5sKG' % 65)\n",
    "print('你的体重为：%s，你的身高为：%s' % (65,75)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "apparent-stroke",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你的体重为：   65KG\n"
     ]
    }
   ],
   "source": [
    "print('你的体重为：%5sKG' % 65)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "rental-kuwait",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你的体重为：65   KG\n"
     ]
    }
   ],
   "source": [
    "print('你的体重为：%-5sKG' % 65)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "trying-attitude",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "incomplete format",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-119-492befd62b4e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'你的成绩百分比为：%.2f\\%'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m20.65\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: incomplete format"
     ]
    }
   ],
   "source": [
    "s = '你的成绩百分比为：%.2f%'\n",
    "print(s % 20.65)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "separated-arnold",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你的成绩百分比为：20.65%\n"
     ]
    }
   ],
   "source": [
    "s = '你的成绩百分比为：%.2f%%'\n",
    "print(s % 20.65)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesbian-toyota",
   "metadata": {},
   "source": [
    "## 索引\n",
    "容器（字符串）中元素的位置，我们称之为索引;   \n",
    "**索引，从左往右，是从0开始   \n",
    "索引，从右往左，是从-1开始**   \n",
    "如果索引超出范围，会报错，`IndexError`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "european-committee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg\n"
     ]
    }
   ],
   "source": [
    "s = 'abcdefg'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "ranging-opinion",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "g\n"
     ]
    }
   ],
   "source": [
    "print(s[6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "modern-upper",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-123-2ea561dd801a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "print(s[7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "adapted-console",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print(s[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "suburban-handy",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f\n"
     ]
    }
   ],
   "source": [
    "print(s[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "structural-glory",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg\n"
     ]
    }
   ],
   "source": [
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "complimentary-complement",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "g\n"
     ]
    }
   ],
   "source": [
    "print(s[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "greatest-qualification",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print(s[-7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "included-interstate",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "string indices must be integers",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-130-674dd3f6894a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: string indices must be integers"
     ]
    }
   ],
   "source": [
    "print(s[2,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "friendly-influence",
   "metadata": {},
   "source": [
    "## 切片   \n",
    "索引，一次只能取一个值，如果想要取多个值，采用切片   \n",
    "切片格式：   \n",
    "`[start:end:step]`:从start位置（包含start），以指定的步长（step），到end位置，但不包含end   \n",
    "当不写start时，表示从头开始   \n",
    "当不写end时，表示到最后  \n",
    "**切片超出返回，会返回一个空，不会报错**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "formal-memory",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg\n"
     ]
    }
   ],
   "source": [
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "interior-ethiopia",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cd\n"
     ]
    }
   ],
   "source": [
    "print(s[2:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "quantitative-mouth",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\n"
     ]
    }
   ],
   "source": [
    "print(s[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "local-appliance",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\n"
     ]
    }
   ],
   "source": [
    "print(s[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "infectious-mouse",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defg\n"
     ]
    }
   ],
   "source": [
    "print(s[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "ideal-territory",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defg\n"
     ]
    }
   ],
   "source": [
    "print(s[3:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "matched-cannon",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "print(s[7:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "subjective-workplace",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[7:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "sapphire-kernel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'def'"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "rubber-laugh",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "print(s[-1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "bored-botswana",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg\n"
     ]
    }
   ],
   "source": [
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "aware-integral",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bcdef\n"
     ]
    }
   ],
   "source": [
    "print(s[1:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "attractive-verification",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bf\n"
     ]
    }
   ],
   "source": [
    "print(s[1:-1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "available-graham",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bfjnr\n"
     ]
    }
   ],
   "source": [
    "s = 'abcdefghijklmnopqrst'\n",
    "print(s[1:-1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "computational-portugal",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tsrqponmlkjihgfe\n"
     ]
    }
   ],
   "source": [
    "print(s[-1:3:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "wrong-wildlife",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tsrqponmlkjihgfedcba\n"
     ]
    }
   ],
   "source": [
    "print(s[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "outdoor-complex",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefghijklmnopqrst\n"
     ]
    }
   ],
   "source": [
    "print(s[:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "antique-interface",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "s = 'abc'\n",
    "print(len(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "contrary-thinking",
   "metadata": {},
   "source": [
    "### 练习\n",
    "指出以下输出的内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "inclusive-airplane",
   "metadata": {},
   "outputs": [],
   "source": [
    "str_b = 'abcde123456'   \n",
    "print(str_b[0])\n",
    "print(str_b[3])\n",
    "print(str_b[11])\n",
    "print(str_b[13])\n",
    "print(str_b[len(str_b)-13])\n",
    "print(str_b[-3])\n",
    "print(str_b[-0])\n",
    "print(str_b[0.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "convertible-success",
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"Python is good\"\n",
    "print(string[1:20])\n",
    "print(string[20])\n",
    "print(string[3:-4])\n",
    "print(string[-10:-3])\n",
    "print(string[:])\n",
    "print(string[::])\n",
    "print(string[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "brazilian-charleston",
   "metadata": {},
   "source": [
    "输出奇数位的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "fossil-processor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13579\n"
     ]
    }
   ],
   "source": [
    "s = '123456789'\n",
    "print(s[::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "facial-young",
   "metadata": {},
   "source": [
    "输出偶数位的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "small-classics",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2468\n"
     ]
    }
   ],
   "source": [
    "print(s[1::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "married-understanding",
   "metadata": {},
   "source": [
    "提取出以下字符串用的一条信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "significant-crown",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ilovetest\n"
     ]
    }
   ],
   "source": [
    "str1 = 'i2sl54ovvvb4e3btere32s56t'\n",
    "print(str1[::3])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "282.173px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
